#include "world.h"

World::World()
{
	// settings and pointers
	settings = Settings::Instance();
	// NOT THREADED
	m_collisionConfiguration = new btDefaultCollisionConfiguration();
	m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
	btVector3 worldAabbMin(-10000,-10000,-10000);
	btVector3 worldAabbMax(10000,10000,10000);
	m_broadphase = new btAxisSweep3 (worldAabbMin, worldAabbMax);
	m_broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
	m_solver = new btSequentialImpulseConstraintSolver;
	m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
	// END NOT THREADED

	m_dynamicsWorld->getSolverInfo().m_solverMode = SOLVER_USE_WARMSTARTING | SOLVER_SIMD | SOLVER_ENABLE_FRICTION_DIRECTION_CACHING;
	m_dynamicsWorld->getSolverInfo().m_numIterations = 8;

	// reset cam
		resetCamera();
}

void World::init()
{
	makeFloor();
}

void World::process()
{
}

void World::draw()
{
	drawfloor();
// 	m_dynamicsWorld->debugDrawWorld();
}

void World::drawWithGrid()
{
	glClear(GL_STENCIL_BUFFER_BIT);
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);

// 	for( unsigned int i=0; i < critters.size(); i++)
// 		critters[i]->draw(true);

// 	for( unsigned int i=0; i < entities.size(); i++)
// 		entities[i]->draw();

	glDisable(GL_STENCIL_TEST);
	glDisable(GL_CULL_FACE);
}

void World::drawfloor()
{
/*	for( unsigned int i=0; i < entities.size(); i++)
		if ( entities[i]->type == WALL )
			entities[i]->draw();*/
}

void World::resetCamera()
{
// 	unsigned int biggest = *worldsizeX;
// 	if ( *worldsizeY > biggest )
// 		biggest = 1.4f**worldsizeY;

// 	camera.position.setOrigin( btVector3( 0.5f**worldsizeX, 1.3f*biggest, 0.5f**worldsizeY) );
// 	camera.position.getBasis().setEulerZYX( -1.5707f, 0.0f, 0.0f ); // 1.5707f  (float)*energy/10

	camera.position.setOrigin( btVector3( -0.5f, -1.3f, -0.5f) );
//  	camera.rotation = Vector3f( 90.0f,  0.0f, 0.0f);
}

void World::makeFloor()
{
	makeDefaultFloor();
}

void World::makeDefaultFloor()
{
// 	for ( int i=0; i < entities.size(); i++ )
// 	{
// 		if ( entities[i]->type == WALL )
// 		{
// 			delete entities[i];
// 			entities.erase(entities.begin()+i);
// 			i--;
// 		}
// 	}

	// Wall Constants
		float WallWidth = 0.5f;
		float WallHalfWidth = WallWidth/2.0f;
		float WallHeight = 2.0f;
		float WallHalfHeight = WallHeight/2.0f;

/*	// Ground Floor
		btVector3 position( *worldsizeX/2.0f, -WallHalfWidth, *worldsizeY/2.0f );
		Wall* w = new Wall( *worldsizeX, WallWidth, *worldsizeY, position, m_dynamicsWorld );
		w->color.r = 0.30f; w->color.g = 0.20f; w->color.b = 0.10f;
		entities.push_back(w);
	
	if ( settings->getCVar("worldwalls") )
	{
		// Left Wall
			position = btVector3 ( 0.0f-WallHalfWidth, WallHalfHeight-WallWidth, *worldsizeY/2.0f );
			w = new Wall( WallWidth, WallHeight, *worldsizeY, position, m_dynamicsWorld );
			w->color.r = 0.34f; w->color.g = 0.25f; w->color.b = 0.11f;
			entities.push_back(w);
		// Right Wall
			position = btVector3 ( *worldsizeX+WallHalfWidth, WallHalfHeight-WallWidth, *worldsizeY/2.0f );
			w = new Wall( WallWidth, WallHeight, *worldsizeY, position, m_dynamicsWorld );
			w->color.r = 0.34f; w->color.g = 0.25f; w->color.b = 0.11f;
			entities.push_back(w);
		// Top Wall
			position = btVector3 ( *worldsizeX/2.0f, WallHalfHeight-WallWidth, 0.0f-WallHalfWidth );
			w = new Wall( *worldsizeX+(WallWidth*2), WallHeight, WallWidth, position, m_dynamicsWorld );
			w->color.r = 0.34f; w->color.g = 0.25f; w->color.b = 0.11f;
			entities.push_back(w);
		// Bottom Wall
			position = btVector3 ( *worldsizeX/2.0f, WallHalfHeight-WallWidth, *worldsizeY+WallHalfWidth );
			w = new Wall( *worldsizeX+(WallWidth*2), WallHeight, WallWidth, position, m_dynamicsWorld );
			w->color.r = 0.34f; w->color.g = 0.25f; w->color.b = 0.11f;
			entities.push_back(w);
	}*/
}

// void World::togglePause()
// {
// 	pause = !pause;
// }

World::~World()
{
// 	for ( unsigned int i=0; i < food.size(); i++ )		delete food[i];
// 	for ( unsigned int i=0; i < entities.size(); i++ )	delete entities[i];
// 	for ( unsigned int i=0; i < critters.size(); i++ )	delete critters[i];
// 	for ( unsigned int i=0; i < walls.size(); i++ )		delete walls[i];

// 	free(retina);

	delete m_collisionConfiguration;
	delete m_dispatcher;
	delete m_broadphase;
	delete m_solver;
	delete m_dynamicsWorld;
	
// 	delete raycast;
// 	delete mousepicker;

// 	omp_destroy_lock(&my_lock1);
// 	omp_destroy_lock(&my_lock2);
}
